package com.chenjj.bigdata.maxcompute;

import java.sql.*;

public class JDBCTester {
    public static void main(String[] args) throws Exception {
        Class.forName("com.aliyun.odps.jdbc.OdpsDriver");
        String url="jdbc:odps:http://service.cn-shanghai.maxcompute.aliyun.com/api?project=Sunline_DataLake&useProjectTimeZone=true";
        String accessId="LTAI5tKyfAn5QQrN2M6j3so7";
        String accessKey="g9Wku15Wl9QLTISVi4WSYomi1PdQ8V";
        Connection conn = DriverManager.getConnection(url, accessId, accessKey);

        Statement stmt = conn.createStatement();
        stmt.execute("set odps.sql.allow.fullscan=true");
        ResultSet rset = stmt.executeQuery("SELECT * FROM idl_person_pro_trans");

        while (rset.next()) {
            System.out.println(rset.getString(1));
        }

        // get meta data
        DatabaseMetaData metaData = conn.getMetaData();
        System.out.println("product = " + metaData.getDatabaseProductName());
        System.out.println("jdbc version = "
                + metaData.getDriverMajorVersion() + ", "
                + metaData.getDriverMinorVersion());
        ResultSet tables = metaData.getTables(null, null, "idl_person_pro_trans", null);
        while (tables.next()) {
            String name = tables.getString("TABLE_NAME");
            System.out.println("inspecting table: " + name);
            ResultSet columns = metaData.getColumns(null, null, name, null);
            while (columns.next()) {
                System.out.println(
                        columns.getString("COLUMN_NAME") + "\t" +
                                columns.getString("TYPE_NAME") + "(" +
                                columns.getInt("DATA_TYPE") + ")");
            }
            columns.close();
        }

        rset.close();
        stmt.close();
        conn.close();
    }
}
